
function clientCmdCt_startControl(%name, %sx, %sy) {
	echo("startControl " @ %name);
	$ct_oldText[%name] = "";
	$ct_sx[%name] = %sx;
	$ct_sy[%name] = %sy;
	$ct_controlActive[%name] = 1;
}
function clientCmdCt_stopControl(%name) {
	echo("stopControl " @ %name);
	$ct_oldText[%name] = "";
	$ct_sx[%name] = "";
	$ct_sy[%name] = "";
	$ct_controlActive[%name] = "";
}
$ct_hexVals[0] = 0; $ct_hexVals[1] = 1; $ct_hexVals[2] = 2; $ct_hexVals[3] = 3; $ct_hexVals[4] = 4; $ct_hexVals[5] = 5; $ct_hexVals[6] = 6; $ct_hexVals[7] = 7; $ct_hexVals[8] = 8; $ct_hexVals[9] = 9; $ct_hexVals[10] = A; $ct_hexVals[11] = B; $ct_hexVals[12] = C; $ct_hexVals[13] = D; $ct_hexVals[14] = E; $ct_hexVals[15] = F;
function ct_toHex2(%v) {
	if(%v<0 || %v>255) { echo("ct_toHex2 - invalid value " @ %v); return; }
	return $ct_hexVals[mFloor(%v/16)] @ $ct_hexVals[%v % 16];
}
function ct_encodeUpdateData(%x, %y, %str) {
	%data = ct_toHex2(%x) @ ct_toHex2(%y) @ ct_toHex2(strLen(%str)) @ %str;
}
function ct_encodeUpdateDataIdx(%name, %i, %str) {
	%sx = $ct_sx[%name];
	%x = %i % %sx;
	%y = mFloor(%i / %sx);
	return ct_encodeUpdateData(%x, %y, %str);
}
function ct_mMax(%a, %b) {
	return %a>%b ? %a : %b;
}
function ct_sendUpdate(%name, %data) {
	if(!$ct_controlActive[%name]) { echo("ct_sendUpdate - control is not active for " @ %name); return; }
	commandToServer('ct_updateScreen', %name, %data);
}
function ct_sendString(%name, %x, %y, %str) {
	if(!$ct_controlActive[%name]) { echo("ct_sendString - control is not active for " @ %name); return; }
	%data = ct_encodeUpdateData(%x, %y, %str);
	ct_sendUpdate(%name, %data);
}
// nice algorithm but fuckin' slow once the screen fills up, probably have to do something else, or maybe this in lua
function ct_sendDiff(%name, %newText) {
	if(!$ct_controlActive[%name]) { echo("ct_sendDiff - control is not active for " @ %name); return; }
	
	%oldText = $ct_oldText[%name];
	%len = ct_mMax(strLen(%newText), strLen(%oldText));
	
	%data = ""; %dataLen = 0;
	%runStr = ""; %runStart = "";
	for(%i=0; %i<%len; %i++) {
		%cn = getSubStr(%newText, %i, 1);
		%co = getSubStr(%oldText, %i, 1);
		if(%cn !$= %co) {
			%runStr = %runStr @ (%cn$="" ? " " : %cn);
			if(%runStart $= "") %runStart = %i;
		}
		if((%cn $= %co || %runLen >= 249) && %runLen > 0) {
			%newData = ct_encodeUpdateDataIdx(%name, %runStart, %runStr);
			%runStr = ""; %runStart = "";
			%newLen = strLen(%newData);
			if(%newLen + %dataLen > 255) {
				ct_sendUpdate(%name, %data);
				%data = "";
				%dataLen = 0;
			}
			%data = %data @ %newData;
			%dataLen += %newLen;
		}
	}
	
	if(%runStart !$= "") {
		%newData = ct_encodeUpdateDataIdx(%name, %runStart, %runStr);
		%runStr = ""; %runStart = "";
		%newLen = strLen(%newData);
		if(%newLen + %dataLen > 255) {
			ct_sendUpdate(%name, %data);
			%data = "";
			%dataLen = 0;
		}
		%data = %data @ %newData;
		%dataLen += %newLen;
	}
	if(%dataLen > 0) {
		ct_sendUpdate(%name, %data);
		%data = "";
		%dataLen = 0;
	}
	$ct_oldText[%name] = %newText;
}

// Testing

function clientCmdCt_inputKey(%name, %idx, %key, %state) {
	//echo("inputKey " @ %name SPC %idx SPC %key SPC %state);
	if(%state) {
		$ct_testStr = $ct_testStr @ %key;
		if(strLen($ct_testStr) >= $ct_sx[%name] * $ct_sy[%name]) {
			$ct_testStr = %key;
		}
		ct_sendDiff(%name, $ct_testStr);
	}
}

